package test;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.events.StartDocument;

public class DWExportXML {

	private String xmlFile;
	protected XMLOutputFactory outputFactory;
	protected XMLEventWriter eventWriter;
	protected XMLEventFactory eventFactory;
	protected StartDocument startDocument;
	
	public DWExportXML (String xmlFile) {
		this.xmlFile = xmlFile;
	}

	public void StartXmlOutput(){
		try {
			// Create a XMLOutputFactory
			outputFactory = XMLOutputFactory.newInstance();
			//Create XMLEventWriter
			eventWriter = outputFactory.createXMLEventWriter(
										new OutputStreamWriter(	new FileOutputStream(xmlFile),
										"UTF8"
								)
					);
			// Create an EventFactory
			eventFactory = XMLEventFactory.newInstance();
			//XMLEvent end = eventFactory.createDTD("\n");
			// Create and write Start Tag
			startDocument = eventFactory.createStartDocument("UTF-8","1.0");
			eventWriter.add(startDocument);
			//eventWriter.add(end);
		} catch (Exception e) {
			System.err.println("Exception DWExportXML.java StartXmlOutput()");
			System.out.println(e);
		}
	}
	
	public void FinishXmlOutput(){
		try {
			eventWriter.flush();
			eventWriter.close();
		} catch (Exception e) {
			System.err.println("Exception DWExportXML.java FinishXmlOutput()");
			System.out.println(e);
		}
	}
/*	
	public void writeNode () {
		// Write the different nodes
		try {
			createNode(eventWriter, "unit", "901");
			createNode(eventWriter, "mode", "1");
		} catch (Exception e) {
			System.out.println(e);
		}
	}
*/
	public void writeNode (DWObject obj) {
		// Write the different nodes
		try {
			if(obj!=null) (new DWExportNode(eventFactory,eventWriter, obj)).export();
			//createNode(eventWriter, "mode", "1");
		} catch (Exception e) {
			System.err.println("Exception DWExportXML.java writeNode()");
			System.out.println(e);
		}
	}
/*
	private void createNode(XMLEventWriter eventWriter, String name, String value) throws XMLStreamException {
	
		XMLEventFactory eventFactory = XMLEventFactory.newInstance();
		XMLEvent end = eventFactory.createDTD("\n");
		XMLEvent tab = eventFactory.createDTD("\t");

		// Create Attribs
		Attribute attribute = eventFactory.createAttribute("version", "1");
	    List attributeList = Arrays.asList(attribute);
	    List nsList = Arrays.asList();

		
		// Create Start node
		StartElement sElement = eventFactory.createStartElement("", "", name, attributeList.iterator(), nsList.iterator() );
		eventWriter.add(tab);
		eventWriter.add(sElement);
		// Create Content
		Characters characters = eventFactory.createCharacters(value);
		eventWriter.add(characters);
		// Create End node
		EndElement eElement = eventFactory.createEndElement("", "", name);
		eventWriter.add(eElement);
		eventWriter.add(end);

	}
*/
   
}

